package com.example.blog.controller;

import com.example.blog.dto.ArticleDTO;
import com.example.blog.dto.CommentDTO;
import com.example.blog.pojo.PageBean;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.User;
import com.example.blog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //    获取所有用户列表
    @GetMapping("/userlist")
    public Result<PageBean<User>> getuserlist(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer role
    ){

        System.out.println("当前页: "+pageNum);
        System.out.println("页面大小: "+pageSize);
        System.out.println("角色: "+role);

        PageBean<User> pb =  adminService.getUserList(pageNum, pageSize, role);
        return Result.success(pb);

    }

    //删除用户
    @DeleteMapping("/userdelete")
    public Result deleteuser(@RequestParam("id")Integer userid){

        System.out.println("用户id: "+userid);
        boolean s = adminService.deleteUser(userid);
        if (s)return Result.success();
        else return Result.error("删除失败");

    }

    //获取所有文章
    @GetMapping("/articlelist")
    public Result<PageBean<ArticleDTO>> getarticlelist(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer status
    ){

        System.out.println("当前页: "+pageNum);
        System.out.println("页面大小: "+pageSize);
        System.out.println("分类: "+categoryId);
        System.out.println("状态: "+status);

        PageBean<ArticleDTO> pb =  adminService.getArticleList(pageNum, pageSize, categoryId, status);
        return Result.success(pb);

    }

    //查看文章内容
    @GetMapping("/articlecontent")
    public Result<String> getarticleContent(@RequestParam("id")Integer id){

        System.out.println("文章id: "+id);
        String content = adminService.getArticleContent(id);
        return Result.success(content);

    }

    //管理员审核文章
    @PatchMapping ("/audit")
    public Result updatearticleststus(@RequestParam("id")Integer id,@RequestParam("message")String message,@RequestParam("status")Integer status){

        System.out.println("当前: "+id);
        System.out.println("消息: "+message);
        System.out.println("状态: "+status);

        boolean s = adminService.updateArticle(id,message,status);
        if(s) return Result.success();
        else return Result.error("更新失败!");


    }


    //获取所有评论
    @GetMapping("/commentlist")
    public Result<PageBean<CommentDTO>> getcommentlist(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false)Integer articleid,
            @RequestParam(required = false)Integer userid
    ){
        System.out.println("当前页: "+pageNum);
        System.out.println("页面大小: "+pageSize);
        System.out.println("文章id: "+articleid);
        System.out.println("用户id: "+userid);

        PageBean<CommentDTO> pb = adminService.getCommentList(pageNum, pageSize, articleid, userid);

        return Result.success(pb);

    }

    //删除评论
    @DeleteMapping("commentdelete")
    public Result deletecomment(@RequestParam("id")Integer id){

        System.out.println("评论id: "+id);
        boolean s = adminService.deleteComment(id);
        if (s)return Result.success();
        else return Result.error("删除失败");

    }



}
